﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Linq.Expressions;
using System.Threading;

namespace LambdaProj
{
    class Program
    {
        public enum SortType
        {
            Ascending,
            Descending
        }

        public delegate bool ComparisonHandler(int first,int second);

        [STAThread]
        static void Main(string[] args)
        {
            //object 
            //DelegateSample.FunDemo();
            //DelegateSample.ActionDemo();
            //List<int> arr = new List<int>() { 1};
            //var delegateList = arr.Find(delegate(int num) {
            //    return num > 2;
            //});
            //int a = arr.Ext();
            //Console.WriteLine(arr.GetType4444());
            //var el = LinqHelper.GetMyEL();
            //List<>
            //int[] arr = { 3,4,2,44,43,998};
            ////BubbleSort(arr, GreaterThan);
            //BubbleSort(arr, (int first, int second) =>
            //{
            //    return first < second;
            //});
            //Print(arr);
            //Express.GetValue();

            //Action<int,int>
            // linq查询
            //IEnumerable<Patent> patents = PatentData.Patents.Where(patent =>
            //    patent.YearOfPublication.StartsWith("1900")
            //);
            //Print(patents);
            //var namelist = PatentData.Patents.Select(u=>u.Title).ToList();
            //Print(namelist);
            ////var patent1900 = PatentData.Patents.Select(u=>u.ToString());
            //IEnumerable<string> fileList = Directory.GetFiles(@"E:\Bitbucket\d2m\DatabaseStructure");
            //var files = fileList.Select(file =>
            //{
            //    FileInfo fileInfo = new FileInfo(file);
            //    return new { FileName = fileInfo.Name, Size = fileInfo.Length };
            //});
            //Print(files);

            //var patentsQuery = (from it in PatentData.Patents
            //                   where it.Title.ToLower() == "bin"&&it.YearOfPublication.StartsWith("1900")
            //                   select it.Title).ToList();

            //var paging = PatentData.Patents.Take(5).Skip(2).ToList();
            //int patentCount = PatentData.Patents.Count(u=>u.Title=="bin");

            //DataTable dt = new DataTable();
            //var rt = (from it in dt.AsEnumerable()
            //         where it["d"].ToString() == "d"
            //         select it).ToList();



            //Console.WriteLine("patent count:{0}",PatentData.Patents.Count());
            //if(PatentData.Patents.Any())
            //{
            //    Console.WriteLine("not null");
            //}
            //int count_1900 = PatentData.Patents.Count(u => u.YearOfPublication.StartsWith("1984"));
            //Console.WriteLine("patent start with 1984:{0}",count_1900);
            //IEnumerable<Patent> patents = PatentData.Patents;
            //bool result = false;
            //patents = patents.Where(patent => {
            //    if (result == patent.YearOfPublication.StartsWith("1984"))
            //  {
            //      Console.WriteLine("\t"+patent);
            //  }
            //  return result;
            //});
            //Console.WriteLine("1、patent prior to the 1900s are:");
            //foreach(var p in patents)
            //{

            //}
            //Console.WriteLine();
            //Console.WriteLine("2、A second list of patents prior to the 1900s.");
            //patents.Count();
            //Console.WriteLine();
            //Console.WriteLine("3、A third list of patents prior to the 1900s.");
            //patents.ToArray();
            //Console.Write("  There are ");
            //Console.WriteLine("{0} patents prior to 1900s",patents.Count());
            //var items = patents.OrderBy(patent => patent.YearOfPublication).ThenBy(patent => patent.Title);
            //Print(items);
            //Console.WriteLine();
            //items = patents.OrderByDescending(patent => patent.YearOfPublication).ThenByDescending(patent => patent.Title);
            //Print(items);
            //FileHelper.Rename();
            //string guidKey = Guid.NewGuid().ToString("N");
            //DataService.RunPooc();
            //LinqHelper.IntoDemo();
            //AnonymousType.CreateType();
            //CorporateData.RunPrint();
            //Console.WriteLine("Join Action:");
            //CorporateData.JoinAction();
            //CorporateData.GroupByDemo();
            //ReflectHelper.GetDtType();
            //Thread.CurrentThread.Name = "System Thread";
            //Console.WriteLine(Thread.CurrentThread.Name+"status:"+Thread.CurrentThread.ThreadState);
            //ThreadSample.Run();
            //ThreadHelper.Run();
            MonitorSample.Run();
            Console.Read();
        } 

        private static void Print<T>(IEnumerable<T> items)
        {
          foreach(T item in items)
          {
              Console.WriteLine(item);
          }
        }

        static void Print(int[] arr)
        {
            for(int i=0;i<arr.Length;i++)
            {
                Console.WriteLine(arr[i]);
            }
        }

        public static bool GreaterThan(int first,int second)
        {
            return first > second;
        }

        public static bool LessThan(int first, int second)
        {
            return first < second;
        }

        public static void BubbleSort(int[] items,ComparisonHandler comparisonHandler)
        {
            int i;
            int j;
            int temp;
            ComparisonHandler compareMethod;

            if(items==null)
            {
                return;
            }

            compareMethod = delegate(int first, int second)
            {
                return first > second;
            };

            for(i=items.Length-1;i>=0;i--)
            {
                for(j=1;j<=i;j++)
                {
                    if (comparisonHandler(items[j-1],items[j]))
                    {
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }

        public static void BubbleSort(int[] items)
        {
            int i;
            int j;
            int temp;

            if (items == null)
            {
                return;
            }

            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (items[j - 1] > items[j])
                    {
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }

        public static void BubbleSort(int[] items,SortType sortType)
        {
            int i;
            int j;
            int temp;

            if (items == null)
            {
                return;
            }

            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    switch(sortType)
                    {
                        case SortType.Ascending:
                            if (items[j - 1] > items[j])
                            {
                                temp = items[j - 1];
                                items[j - 1] = items[j];
                                items[j] = temp;
                            }
                            break;
                        case SortType.Descending:
                            if (items[j - 1] < items[j])
                            {
                                temp = items[j - 1];
                                items[j - 1] = items[j];
                                items[j] = temp;
                            }
                            break;

                    }
                }
            }
        }
    }



    public class DelegateSample
    {
        delegate void TestDelegate(string s);
        /// <summary>
        /// public delegate TResult Func<TArg0, TResult>(TArg0 arg0)
        /// </summary>
        public static void FunDemo()
        {

            TestDelegate testDel = x => {
                string s = x.ToString();
                Console.WriteLine(s);
            };
            testDel("ff");
            Func<int, int> compute = x => x * x;
            int sum = compute(5);
            Console.WriteLine(sum);
        }

        public static void ActionDemo()
        {
            Action<int> printNum = x => {
                Console.WriteLine(x);
            };
            printNum(33);
        }

        public static int GetMax(int num,Func<int,int> f)
        {

            Func<int, int> fun = x => x + 1;
            int result = fun(10);
            return num;
        }

        public static int GetLen()
        {
            int[] arr = { 1,2,3,3,3,3};
            int len = arr.Where(u => u > 2).Count();
            return len;
        }
    }
}
